package com.newsproject.controller;

import com.newsproject.pojo.*;
import com.newsproject.service.EmailService;
import com.newsproject.service.UserService;

import io.swagger.annotations.*;

import lombok.extern.slf4j.Slf4j;

import org.apache.ibatis.annotations.Param;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;

import springfox.documentation.annotations.ApiIgnore;

import javax.servlet.http.HttpSession;
import java.util.List;
import java.util.Map;

@Slf4j
@CrossOrigin
@Controller
@RequestMapping("/user")
@Api(tags = "登录、注册、主页")
public class UserController {

    @Autowired
    private UserService userService;
    @Autowired
    private EmailService emailService;



    @ApiOperation("渲染主页")
    @GetMapping("/recommendPage")
    @ApiResponses(value = {@ApiResponse(code = 200,
            message = "1.posts：是一个集合，查询的10条新闻咨询，2.userInfo：用户信息，3.todayHotSearch：每日热搜的post，4.totalHotSearch：总热搜的post",
            response =String.class )})
    public String redirectToMainPage(HttpSession session, Model model) {
        return userService.redirectToMainPage(session, model);
    }

    @ApiIgnore
    @GetMapping("/login")
    public String login() {
        return "/login";
    }


    @ApiIgnore
    @GetMapping("/register")
    public String register() {
        return "register";
    }

    @ApiIgnore
    @GetMapping("/admin")
    public String admin() {
        return "admin";
    }



    @ApiResponses(value= {@ApiResponse (code = 200,message = "1.email：邮箱回显，2.code:验证码回显",response =String.class )})
    @ApiOperation(value = "登录",notes = "要传两个参数，一个是邮箱，一个是密码")
    @PostMapping("/login")
    public ModelAndView login(@ApiParam(value = "邮箱")@RequestParam("email") String email ,
                              @ApiParam("密码") @RequestParam("password") String password ,
                              @ApiIgnore HttpSession  session) {
        return userService.login(email, password, session);
    }
    @ApiOperation(value = "退出登录",notes = "退出登录")
    @ApiResponses(value = {@ApiResponse(code = 200,message = "1.message：提示信息",response =String.class )})
    @GetMapping("/logout")
    public String logout(@ApiIgnore HttpSession session){
        session.removeAttribute("userInfo");
        return "redirect:/user/recommendPage";
    }
    /*
     * 邮箱验证码注册
     * */
    @ApiOperation(value = "邮箱验证码注册",notes = "邮箱验证码注册")
    @ApiResponses(value = {@ApiResponse(code = 200,message = "1.email：邮箱回显，2.password:密码回显,3.code:验证码回显",response =String.class ) })
    @PostMapping("/register")
    public ModelAndView register(@ApiParam("邮箱")@RequestParam("email") String email ,
                                 @ApiParam("验证码")@RequestParam("code") String code ,
                                 @ApiParam("密码")@RequestParam ("password") String password,
                                 HttpSession session
                                 ){
        return   userService.register(email, code, password,session);
    }

    @ApiOperation("注册发送验证码")
    @GetMapping("/sendCode")
    @ResponseBody
    public Result<VerificationCode> sendCode(@ApiParam(value = "邮箱") @RequestParam("email") String email,
                                             HttpSession session
     ){
          return emailService.sendEmail(email,session);
    }
    @ApiOperation("忘记密码或者更改密码发送验证码")
    @GetMapping("/sendCodeToForgetPassword")
    @ResponseBody
    public Result<String> sendCodeToForgetPassword(@RequestParam("email") String email,
                                                   HttpSession session){
        return emailService.sendCode(email,session);
    }


   /* @ApiOperation("忘记密码")
    @ApiResponses(value = {@ApiResponse(code = 200,message = "1.email：邮箱回显，2.password:密码回显,3.code:验证码回显",response =String.class )})
    @PostMapping("/forgetPassword1")
    public ModelAndView forgetPassword(@RequestParam("email") String email ,
                                       @RequestParam("password")String password,
                                       @RequestParam("code") String code,
                                       HttpSession session){
        return userService.updatePassword(email, code, password,session);
    }
*/



    @ApiOperation(value = "查询每日热搜",notes = "需要传递页数,刷新第二页开始")
    @GetMapping("/dailyHotSearch/{pageNum}")
    @ApiResponses(value = {@ApiResponse(code = 200,
            message = "1.todayHotSearch：每日热搜的post",
            response =String.class )})
    @ResponseBody
    public Result<List<HotSearch>> dailyHotSearch(@Param("页数")@PathVariable("pageNum")Integer pageNum){
        return userService.getTodayHotPost(pageNum);
    }

    @ApiOperation("忘记密码")
    @ApiResponses(value = {@ApiResponse(code = 200,message = "1.email：邮箱回显，2.password:密码回显,3.code:验证码回显",response =String.class )})
    @PostMapping("/forgetPassword")
    @ResponseBody
    public Result forgetPassword1(@RequestParam("email") String email ,
                                       @RequestParam("password")String password,
                                       @RequestParam("code") String code,
                                       HttpSession session){
        return userService.updatePassword1(email, code, password,session);
    }






}
